<?php

class Raid_Form_RaidForm extends Pldkp_Form 
{
    const DATE_FORMAT = 'YYYY-MM-dd HH:mm:ss';
    
    protected $raid = null;
    
    public function __construct(Raid_Model_RaidRow $raid = null, $options = null)
    {
        $this->raid = $raid;
        parent::__construct($options);
    }
    
    public function init()
    {
        $this->setName('create_raid');
        
        $raidLeader = new Pldkp_Form_Element_AutoSelector('raid_leader_user_id');
        $raidLeader->setLabel('Raid Leader')
                   ->setTable('User_Model_Users')
                   ->setColumns('id', 'name')
                   ->setJQueryParams(array(
                         'url' => Zend_Controller_Front::getInstance()->getBaseUrl() . '/user/ajax/get-users',
                         'minChars' => 1,
                         'extraParams' => array( 'raid_leaders' => 1)))
                   ->setRequired(true)
                   ->addValidator('Db_RecordExists', false, array('users','id'));
                   
        $dungeon = new Pldkp_Form_Element_MultiSelector('dungeon_ids');
        $dungeon->setLabel('Dungeons')
                ->setRequired(true);
  
        $_dungeons = array();
        foreach (Raid_Model_Dungeons::getAll() as $_d) {
            $_dungeons[$_d->getReleaseName()][$_d->id] = $_d->name;
        }
        $dungeon->setMultiOptions($_dungeons);
        
        $start = new Pldkp_Form_Element_DateTimePicker('start_time');
        $start->setLabel('Start')
              ->setRequired(true)
              ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        $end = new Pldkp_Form_Element_DateTimePicker('end_time');
        $end->setLabel('End')
            ->setRequired(true)
            ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        $signup = new Pldkp_Form_Element_DateTimePicker('signup_deadline');
        $signup->setLabel('Signup Dealine')
               ->setRequired(true)
               ->addValidator('DateTime',false,array(self::DATE_FORMAT));
        
        $note = new Zend_Form_Element_Textarea('note');
        $note->setLabel('Note');
        
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($this->raid ? 'Edit' : 'Create');
        
        $this->addElements(array($raidLeader, $dungeon, $start, $end, $signup, $note, $submit));
    }
    
    public function setDefaultDate(Zend_Date $date)
    {
        $start = $date->copyPart(null);
        $start->set('20:00:00', Zend_Date::TIMES);
        
        $finish = $date->copyPart(null);
        $finish->set('23:00:00', Zend_Date::TIMES);
        
        $signup = $start->copyPart(null);
        $signup->subDay(1);

        $this->setDefaults(array(
            'start_time'      => $start->toString(self::DATE_FORMAT),
            'end_time'        => $finish->toString(self::DATE_FORMAT),
            'signup_deadline' => $signup->toString(self::DATE_FORMAT)
        ));
    }
    
}